package com.brilliance.project.common.report;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.brilliance.project.common.exception.BaseJasperReportException;
import com.brilliance.project.common.exception.ErrorReferenceReport;

/**
 * Page/class Name: HtmlReportExportImpl<br />
 * Title: 导出 Html 报表<br />
 * Description: 导出 Html 报表<br />
 * Copyright: Copyright(c) Brilliance Technology.<br />
 * 
 * @author HLJ
 * @version 1.0
 * @created 2012/12/2
 */
public class HtmlReportExportImpl implements IBaseReportExport {
	private static Log log = LogFactory.getLog(HtmlReportExportImpl.class);

	/**
	 * 导出报表
	 * 
	 * @param request
	 * @param response
	 * @param jasperPrint
	 * @param fileNam
	 * @throws Exception
	 */
	public void export(HttpServletRequest request,
			HttpServletResponse response, JasperPrint jasperPrint,
			String fileNam) {
		// TODO Auto-generated method stub
		PrintWriter printWriter = null;

		try {
			printWriter = response.getWriter();
		} catch (IOException ioe) {
			ioe.printStackTrace();
			log.error(ioe.toString());
		} finally {
			if (printWriter != null) {
				try {
					printWriter.close();
				} catch (Exception ex) {
					log.error(ex.toString());
					ex.printStackTrace();
				} /* end of try ... catch */
			}
		} /* end of try ... catch ... finally */

		try {
			response.setContentType(ReportConstant.CONTENT_TYPE_HTML);
			response.setCharacterEncoding(ReportConstant.CHARACTER_ENCODING_UTF_8);
			// 使用 JRHtmlExproter 导出 Html 格式报表
			JRHtmlExporter exporter = new JRHtmlExporter();
			// 将Servlet生成的JasperReport对像导出成HTML格式
			// 导出所用的Servlet为JasperReport自带的ImageServle
			request.getSession().setAttribute(
					ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
					jasperPrint);
			// 如果已经有了jasper_print对象,可以压缩后将其输出
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			// 要把报表输出到的文件
			exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
					printWriter);
			// UTF-8 格式
			exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,
					ReportConstant.CHARACTER_ENCODING_UTF_8);
			// exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
			// "./servlets/image?image=");
			// 是否去除行间的空白
			// exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
			// new Boolean(true));
			// 输出引擎是否用小图片矫正
			// exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
			// Boolean.FALSE);
			// 导出
			exporter.exportReport();
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
			throw new BaseJasperReportException(
					ErrorReferenceReport.RESPONSE_GET_PRINTWRITER_ERROR);
		}
	}

	public void view(HttpServletRequest request, HttpServletResponse response,
			Map params, JRDataSource dataSource, String fileName) {
		// TODO Auto-generated method stub
		// 此功能尚未实作
		throw new BaseJasperReportException(
				ErrorReferenceReport.METHOD_NOT_IMPLEMENT);
	}

	public void viewList(HttpServletRequest request,
			HttpServletResponse response, Map params,
			List<? extends JRDataSource> dataSourceList, String fileName) {
		// TODO Auto-generated method stub

	}

	@Override
	public void export(JasperPrint jasperPrint, String fileName) {
		// do nothing

	}

	@Override
	public void export(List<JasperPrint> jasperPrintList, String fileName,
			String[] sheetNames) {
		// TODO Auto-generated method stub

	}

	@Override
	public InputStream exportForInputStream(JasperPrint jasperPrint,
			String fileName) {
		// TODO Auto-generated method stub
		return null;
	}

}
